//
//  JMHomeVController.m
//  jiMuOperate
//
//  Created by 胡梅方 on 16/4/28.
//  Copyright © 2016年 jinXinFengLi. All rights reserved.
//

#import "JMHomeVController.h"
#import "JMTeamTVController.h"
#import "JMEquipmentTVController.h"
#import "JMHomeView.h"
#import "JMHomeInfo.h"
#import "JMUserBasicInfo.h"
#import "JMTaskTVController.h"
#import "JMGetTeamDataInfo.h"
#define HomeVH 190
@interface JMHomeVController ()
@property(nonatomic,weak)UIButton *taskButton;
@property(nonatomic,weak)UIButton *equipmentButton;
@property(nonatomic,weak)UIButton *teamButton;
@property(nonatomic,weak)JMHomeView *homeView;

@end

@implementation JMHomeVController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    HIDDENNAV(YES);
    //    添加头部view
    [self addHeaderView];
    //    添加按钮
    [self addButtonView];
    

}


- (void)requestPineConeParkDataWithToken:(NSString *)token
{
    NSString *url = [NSString stringWithFormat:@"%@?Token=%@",JMURLHomeData,token];
    NSArray *arrayR = @[@"",url,@"GET"];
    [Request startRequestWithArray:arrayR FinishedBlock:^(NSData *data) {
        NSDictionary *dictR = [JMData dictAndTestData:data];
        JMHomeInfo *numdata = [JMHomeInfo objectWithKeyValues:dictR];
        JMHomeData *homeData = [JMHomeData objectWithKeyValues:numdata.data];
        self.homeView.sceneLabel.text = homeData.PineConeParkName;
        self.homeView.teamRankNum.text = homeData.PineConeParkRank;
        
    } FailBlock:^(NSString *string) {
        //        [timer invalidate];
        SHOWALERT(@"服务器繁忙，请梢后重试");
    }];
    
}

- (void)requestUserBasicInfoWithToken:(NSString *)token
{
    NSString *url = [NSString stringWithFormat:@"%@%@",JMURLUserBasicInfo,token];
    NSArray *arrayR = @[@"",url,@"GET"];
    [Request startRequestWithArray:arrayR FinishedBlock:^(NSData *data) {
        NSDictionary *dict = [JMData dictAndTestData:data];
        JMUserBasicInfo *userInfo = [JMUserBasicInfo objectWithKeyValues:dict];
        JMUserBasicData *userData = [JMUserBasicData objectWithKeyValues:userInfo.data];
        UserEntity *user = [UserEntity shardUserInfo];
        [JMData requestOrSetImgWithImageView:self.homeView.headImageV ProductImageUrl:userData.HeadImg];
        if (userData.NickName == NULL) {
            SHOWALERT(@"请点击任务按钮，设置呢称");
        }
        self.homeView.nameLabel.text = userData.NickName;
        self.homeView.gradeLabel.text = userData.UserLevel;
        user.nickName = userData.NickName;
        user.IsExtend = userData.IsExtend;
        user.IsStrategyPeople = userData.IsStrategyPeople;
        user.PineConeGreen = userData.PineConeGreen;
        user.PineNut = userData.PineNut;
        user.UserLevel = userData.UserLevel;
        user.PineConeRedCount = userData.PineConeRedCount;
        user.PineConeGreenAva = userData.PineConeGreenAva;
        user.PineConeRedAva = userData.PineConeRedAva;
        
       
        
    } FailBlock:^(NSString *string) {
        SHOWALERT(@"服务器繁忙，请梢后重试");
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSString *token = [UserEntity shardUserInfo].access_token;
    [self requestPineConeParkDataWithToken:token];
    [self requestUserBasicInfoWithToken:token];
    [self requestGetTeamDataWithToken:token];
    HIDDENNAV(YES);
    
    
}
-(void)addHeaderView{
    JMHomeView *homeView = [[JMHomeView alloc]initWithFrame:CGRectMake(0, 0, 320, HomeVH)];
    [self.view addSubview:homeView];
    self.homeView = homeView;
    homeView.frame = [ToolViewAndData MyAutoLayout:homeView];
  
};
-(void)addButtonView{
    CGFloat BtnX = 50;
    CGFloat BtnY = 210;
    CGFloat BtnW = 40;
    UIButton *taskBtn = [ARC createButtonWithFrame:CGRectMake(BtnX, BtnY, BtnW, BtnW) Target:self Sel:@selector(taskBtnClick:) Title:nil NormalImageName:@"pic_task"];
    self.taskButton = taskBtn;
    [self.view addSubview:taskBtn];
    UIButton *taskWordBtn = [ARC createButtonWithFrame:CGRectMake(BtnX, BtnY+BtnW, BtnW, BtnW) Target:self Sel:@selector(taskBtnClick:) Title:@"任务"];
    
    [self.view addSubview:taskWordBtn];

    
    UIButton *equipBtn = [ARC createButtonWithFrame:CGRectMake(BtnX*3, BtnY, BtnW, BtnW) Target:self Sel:@selector(equipBtnClick:) Title:nil NormalImageName:@"pic_equipment"];
    self.equipmentButton = equipBtn;
    [self.view addSubview:equipBtn];
    UIButton *equipWordBtn = [ARC createButtonWithFrame:CGRectMake(150,250, BtnW, BtnW) Target:self Sel:@selector(equipBtnClick:) Title:@"装备"];
    [self.view addSubview:equipWordBtn];
    
    
    UIButton *teamBtn = [ARC createButtonWithFrame:CGRectMake(250, 210, BtnW, BtnW) Target:self Sel:@selector(teamBtnClick:) Title:nil NormalImageName:@"pic_team"];
    self.teamButton = teamBtn;
    [self.view addSubview:teamBtn];
    UIButton *teamWordBtn = [ARC createButtonWithFrame:CGRectMake(250, 250, BtnW, BtnW) Target:self Sel:@selector(teamBtnClick:) Title:@"团队"];
    [self.view addSubview:teamWordBtn];
    
    [taskWordBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [equipWordBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [teamWordBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    teamBtn.frame = [ToolViewAndData MyAutoLayout:teamBtn];
    teamWordBtn.frame = [ToolViewAndData MyAutoLayout:teamWordBtn];
    taskBtn.frame = [ToolViewAndData MyAutoLayout:taskBtn];
    taskWordBtn.frame = [ToolViewAndData MyAutoLayout:taskWordBtn];
    equipBtn.frame = [ToolViewAndData MyAutoLayout:equipBtn];
    equipWordBtn.frame = [ToolViewAndData MyAutoLayout:equipWordBtn];
    
    
    
}
-(void)taskBtnClick:(UIButton*)btn{

    JMTaskTVController *taskVC = [[JMTaskTVController alloc]init];
    [self.navigationController pushViewController:taskVC animated:YES];
}
-(void)equipBtnClick:(UIButton*)btn{
    JMEquipmentTVController *equipVC = [[JMEquipmentTVController alloc]init];
    [self.navigationController pushViewController:equipVC animated:YES];
    
}
-(void)teamBtnClick:Button{

    JMTeamTVController *teamVc = [[JMTeamTVController alloc]init];
    [self.navigationController pushViewController:teamVc animated:YES];
    
}
-(void)requestGetTeamDataWithToken:(NSString*)token
{
    NSString *url = [NSString stringWithFormat:@"%@%@",JMURLGetTeamData,token];
    NSArray *arrayR = @[@"",url,@"GET"];
    [Request startRequestWithArray:arrayR FinishedBlock:^(NSData *data) {
        NSDictionary *dict = [JMData dictAndTestData:data];
        JMGetTeamDataInfo *teamData = [JMGetTeamDataInfo objectWithKeyValues:dict];
        JMGetTeamDataReturn *teamDataReturn = [JMGetTeamDataReturn objectWithKeyValues:teamData.data];
        UserEntity *user = [UserEntity shardUserInfo];
        user.CommuneNumber = teamDataReturn.CommuneNumber;
        user.MyBindNumber = teamDataReturn.MyBindNumber;
        user.PineConeParkRank = teamDataReturn.PineConeParkRank;
        user.StrategyPeopleNum= teamDataReturn.StrategyPeopleNumber;
     } FailBlock:^(NSString *string) {
        SHOWALERT(@"服务器繁忙，请梢后重试");
    }];
}
@end
